//
//  ZuzhiSearchViewController.m
//  ZhuoxinAgriculture
//
//  Created by 左忠飞 on 16/11/11.
//  Copyright © 2016年 KKING. All rights reserved.
//

#import "ZuzhiSearchViewController.h"
#import "Masonry.h"


@interface ZuzhiSearchViewController () <UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *footerView;
@property (nonatomic, strong) UISearchBar *searchBar;
@property (nonatomic, strong) NSArray *sourceArray;


@end

@implementation ZuzhiSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.sourceArray = @[];
    self.title = @"组织列表";
    
    [self setupUI];
}

- (void)setupUI
{
    [self.view addSubview:self.tableView];
    self.tableView.tableHeaderView = self.searchBar;
    self.tableView.tableFooterView = self.footerView;
    self.footerView.bounds = CGRectMake(0, 0, kScreenW, kI(120.0));
    
    
}


#pragma mark lazy load
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH-64) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableFooterView = [UIView new];
        _tableView.backgroundColor = UIColorFromRGBA(0xf5f5f5);
    }
    return _tableView;
}

- (UISearchBar *)searchBar
{
    if (!_searchBar) {
        _searchBar = [[UISearchBar alloc] init];
        [_searchBar sizeToFit];
        _searchBar.placeholder = @"搜索农场/大户/合作社";
        _searchBar.delegate = self;
    }
    return _searchBar;
}


#pragma mark searchBar delegate
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    self.searchBar.showsCancelButton = YES;
    for (id cencelButton in [searchBar.subviews[0] subviews])
    {
        if([cencelButton isKindOfClass:[UIButton class]])
        {
            UIButton *btn = (UIButton *)cencelButton;
            [btn setTitle:@"取消"  forState:UIControlStateNormal];
            btn.enabled = YES;
            btn.titleLabel.font = [UIFont systemFontOfSize:kI(28.0)];
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
    }
    return YES;
}

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
    self.searchBar.showsCancelButton = NO;
    return YES;
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    [self.searchBar endEditing:YES];
}

#pragma mark - tableview dataSource/delegate method
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.sourceArray.count + 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0) {
        ZuzhiHistoryCell *cell = [ZuzhiHistoryCell historyCellWithTableView:tableView];
        cell.historyLabels = @[@"旭日农场", @"李四", @"王五", @"小六", @"农业合作社", @"李大白", @"周嘉嘉", @"傅天"];
        return cell;
    } else {
        UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellId"];
        
        return cell;
    }
}

- (UIView *)footerView
{
    if (!_footerView) {
        _footerView = [UIView new];
        UIButton *btn = [UIButton new];
        btn.layer.borderWidth = 1.0;
        btn.layer.borderColor = UIColorFromRGBA(0xafafaf).CGColor;
        [btn setTitleColor:UIColorFromRGBA(0x7d7c82) forState:UIControlStateNormal];
        [btn setTitle:@"清除搜索历史" forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:kI(24.0)];
        [btn addTarget:self action:@selector(clearHistory) forControlEvents:UIControlEventTouchUpInside];
        btn.layer.cornerRadius = 2;
        btn.layer.masksToBounds = YES;
        [_footerView addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(_footerView);
            make.top.mas_equalTo(_footerView).offset(kI(60.0));
            make.width.mas_equalTo(kI(200.0));
            make.centerX.mas_equalTo(_footerView);
        }];
    }
    return _footerView;
}

- (void)clearHistory
{
    NSLog(@"clear history");
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return kI(284.0);
    }
    return 44.0;
}

@end
